package com.abigdreamer.arkflow;

import java.util.HashMap;
import java.util.List;

public class WorkflowJsonToXml {

	public WorkflowJsonToXml() {
	}

	public String toXml(List<WorkflowNode> states,
			HashMap<String, String> idNameMap, WorkflowNode workflow) {
		StringBuffer sb = new StringBuffer(
				"<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
		sb.append("<process name=\"").append(workflow.getName())
				.append("\" xmlns=\"http://jbpm.org/4.4/jpdl\">");
		for (WorkflowNode process : states) {
			if (process.getType().equals("start")
					|| process.getType().equals("end")) {
				sb.append("<").append(process.getType());
			} else if (process.getType().equals("decision")) {
				sb.append("<").append(process.getType());
				sb.append(" expr=").append("\"#{").append(process.getExpr())
						.append("}\"");
				sb.append(" g=").append("\"").append(process.getG())
						.append("\"");
				sb.append(" name=").append("\"").append(process.getName())
						.append("\">");
			} else if (process.getType().equals("task")) {
				if (process.getAssignType().equals("0")) {
					// 角色
					sb.append("<").append(process.getType()).append(" name=\"")
							.append(process.getName());
					sb.append("\" form=").append("\"")
							.append(process.getForm()).append("\"");
					sb.append(" g=\"").append(process.getG()).append("\" >");
					sb.append("<assignment-handler ").append("class=\"")
							.append(ProjectConstants.ASSIGNMENT_HANDLER);
					sb.append("\" >");
					sb.append("<field name=\"")
							.append(ProjectConstants.ASSIGNMENT_HANDLER_FIELD_ROLDIDS)
							.append("\" >");
					sb.append("<string value=\"")
							.append(process.getAssignValue()).append("\"/>");
					sb.append("</field>");
					sb.append("</assignment-handler>");
				} else if (process.getAssignType().equals("1")) {
					// 指定审批人
					sb.append("<").append(process.getType());
					sb.append(" assignee=").append("\"")
							.append(process.getAssignee()).append("\"");
					sb.append(" form=").append("\"").append(process.getForm())
							.append("\"");
					sb.append(" g=").append("\"").append(process.getG())
							.append("\"");
					sb.append(" name=").append("\"").append(process.getName())
							.append("\">");
				} else if (process.getAssignType().equals("2")) {
					// 发起人
					sb.append("<").append(process.getType());
					sb.append(" assignee=").append("\"#{")
							.append(ProjectConstants.ASSIGNMENT_PROMOTER)
							.append("}\"");
					sb.append(" form=").append("\"").append(process.getForm())
							.append("\"");
					sb.append(" g=").append("\"").append(process.getG())
							.append("\"");
					sb.append(" name=").append("\"").append(process.getName())
							.append("\">");
				}
			}
			if (process.getType().equals("end")) {
				sb.append(" g=").append("\"").append(process.getG())
						.append("\"");
				sb.append(" name=").append("\"").append(process.getName())
						.append("\"/>");
			} else if (process.getType().equals("start")) {
				sb.append(" g=").append("\"").append(process.getG())
						.append("\"");
				sb.append(" name=").append("\"").append(process.getName())
						.append("\">");
			}
			if (process.getPaths() != null && process.getPaths().size() > 0) {
				for (WorkflowPath path : process.getPaths()) {
					sb.append("<transition");
					sb.append(" g=\"").append(path.getG()).append("\"");
					if (process.getPaths().size() > 1) {
						sb.append(" name=").append("\"").append(path.getName())
								.append("\"");
					}
					sb.append(" to=\"").append(idNameMap.get(path.getTo()));
					sb.append("\"/>");
				}
			}
			if (!process.getType().equals("end")) {
				sb.append("</").append(process.getType()).append(">");
			}
		}

		sb.append("</process>");
		String xml = sb.toString();
		return JsonToXML.prettyFormat(xml, 4);
	}
}
